var basePage = require('../../../common/BasePage');
var baseList = require('../../../common/BaseList');
Page({
	behaviors: [basePage, baseList],
	data: {
		checkMobile: false,
		active: 0,
		addCart: false,
		pdc: {},
		nums: 1,
		sku: 0,
		product_id: 0,
		product_name: "",
		current_page: 0,
		pages: 1,
		//评价
		CommentArr: [],
		count: 0,
		CommentCurrent_page: 0,
		CommentPages: 1,
		//分类
		catArr: [],
		supplier_id:0,
		//店铺详情
		storeInfo:{}
	},
	onLoad(opt) {
		console.log(opt)
		let catArr = getApp().globalData.cateArr
		var reqData = {
			wh_id: getApp().globalData.warehouse.wh_id
		}
		this.setData({
			reqData: reqData,
			catArr: catArr,
			supplier_id:opt.supplier_id
		})
		this.getDetail()
		
		this.getCommentArr()
		// this.req_cart(warehouse.wh_id)
	},
	onShow() {

	},
	getDetail(){
		this.request({
			url:this.data.host+"/home/supplier/detail",
			data:{
				supplier_id: this.data.supplier_id,
			}
		},res=>{
			var info=res.data
			info.thumb=this.get_thumb(info.logo)
			this.setData({
				storeInfo:info
			})
			this.req_cat_data();
		})
	},
	getCommentArr() {
		if (this.data.CommentCurrent_page == this.data.CommentPages) {
			return;
		}
		this.request({
			url: this.data.host + '/home/supplier/comment',
			data: {
				supplier_id: this.data.supplier_id,
				page: (this.data.CommentCurrent_page * 1) + 1,
			}
		}, res => {
			let list = this.data.CommentArr
			res.data.map(val => {
				let that = this;
				val.product_thumb = this.get_thumb(val.product_img);
				val.face = this.get_thumb(val.user_face);
				let photoArr = [];
				val.photos.map(photo => {
					photoArr.push(that.get_thumb(photo))
				})
				val.photoArr = photoArr;
				val.videos.map(video => {
					val.video = that.data.imgHost + "/" + video
				})
			});
			res.current_page == 1 ? list = res.data : list = list.concat(res.data);
			console.log("list",list)
			this.setData({
				CommentArr: list,
				CommentCurrent_page: res.current_page,
				CommentPages: res.pages,
				count: res.count
			});
			wx.stopPullDownRefresh({
				success: (res) => {},
			})
		});
	},
	req_cat_data() {
		if (this.data.current_page == this.data.pages) {
			return;
		}
		this.request({
			url: this.data.host + '/home/supplier/product/index',
			data: {
				wh_id: this.data.storeInfo.wh_id,
				supplier_id:this.data.supplier_id,
				page: (this.data.current_page * 1) + 1,
			}
		}, res => {
			this.get_pdcs_ok(res);
		});
	},
	get_pdcs_ok(res) {
		let list = this.data.itemArr
		res.data.map(val => {
			val.thumb = this.get_thumb(val.product_img);
			// console.log("val.thumb",val.thumb);
		});
		res.current_page == 1 ? list = res.data : list = list.concat(res.data);
		this.setData({
			itemArr: list,
			current_page: res.current_page,
			pages: res.pages
		});
		wx.stopPullDownRefresh({
			success: (res) => {},
		})
	},

	nums_change(e) {
		// this.data.nums = e.detail;
		this.setData({
			nums: e.detail
		})
		// console.log("nums_change", e)
	},
	onChange(e) {
		this.setData({
			active: e.detail.index
		})
	},
	init_val(val, index, arr) {
		val.thumb = this.get_thumb(val.product_img);
	},
	/**
	 * 添加购物车按钮点击事件
	 * @param {*} param0 
	 */
	add_cart_click(e) {
		let pdc = e.detail;
		this.setData({
			nums: 1,
			addCart: true,
			pdc: pdc,
			sku: pdc.sku,
			product_id: pdc.pid,
			product_name: pdc.product_name,
			// carTitle:e.currentTarget.dataset.titlt

		});
	},
	/**
	 * 添加产品到购物车
	 */
	add_cart() {
		console.log("add_cart", this.data.nums, this.data.product_id);
		this.request({
			url: this.data.host + '/home/cart/addToCart',
			data: {
				pid: this.data.product_id,
				nums: this.data.nums,
			},
			method: 'post'
		}, res=>{
			this.setData({
				addCart:false
			})
			wx.showToast({
				title: res.msg,
				icon: 'success'
			})
		});

	},
	onCloseCart(){
		this.setData({
			addCart:false
		})
	},
	//立即购买
	buy_good() {
		console.log("立即购买", this.data.pdc)
		let data = {
			pid: this.data.product_id,
			nums: this.data.nums,
			wh_id: getApp().globalData.warehouse.wh_id,
			from :"flower"
		}

		this.request({
			url: this.data.host + '/home/flower/preInfo',
			data: data,
			method: 'get'
		}, res => {
			this.setData({
				addCart:false
			})
			res.data.cashier_decca = this.get_thumb(res.cashier_decca, 'wide');
			let that = this;
			wx.navigateTo({
				// url: '/pages/order_confirm/order_confirm',
				url: '/pages/neworder_confirm/order_confirm',
				success: function (obj) {
					// 通过eventChannel向被打开页面传送数据

					obj.eventChannel.emit('orderData', {
						wh_id: getApp().globalData.warehouse.wh_id,
						// index: that.data.areaData[that.data.tabIndex].index,
						data: res.data,
						m_addr: res.m_addr,
						activeCar: "flower",
						wuliu: res.data.freight_list,
						specialList: res.data.extra_list,
						pack_list: res.data.pack_list,
						buyType:1,
						order_pei_type:res.order_pei_type
					})
				}
			})
		});
	},
	onShareAppMessage(res) {
		// console.log("onShareAppMessage", res);
		let path = "/pages/cls/index/index?uid=" + getApp().globalData.userInfo.uid;
		let title = "";
		if (res && res.target) {
			let pid = res.target.dataset.pid;
			let product_id = res.target.dataset.product_id;
			if (pid) {
				path = '/pages/cls/pdcList/Index?pid=' + pid + '&product_id=' + product_id + '&uid=' + getApp()
					.globalData.userInfo.uid;
				title = "您的朋友向你推荐了一款鲜花"
			}
		}
		return {
			title: title,
			path: path,
			// promise 
		}
	},

	/**触底加载下一页 */
	onReachBottom() {
		if (this.data.active == 0) {
			this.req_cat_data()
		} else if (this.data.active == 1) {
			this.getCommentArr()
		}

	},
	onPullDownRefresh() {
		console.log("下拉刷新");
		if (this.data.active == 0) {
			this.data.current_page = 0;
			this.req_cat_data()
		} else if (this.data.active == 1) {
			this.data.CommentCurrent_page = 0
			this.getCommentArr()
		}

	},
})
